package com.outstanding.his.patient.dao.impl;

import java.util.Collection;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.outstanding.his.patient.dao.IPatientDao;
import com.outstanding.his.patient.domain.Patient;

@Repository
public class HibernatePatientDao extends HibernateDaoSupport implements
		IPatientDao {

	public Patient submitPatient(Patient patient)
			throws DataAccessException {
		this.getHibernateTemplate().saveOrUpdate(patient);
		return patient;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Patient> getPatientList() throws DataAccessException {
		Collection<Patient> patients = this.getSession().createQuery("from Patient")
				.list();
		return patients;
	}

	@Override
	public Patient getPatient(final Integer patientId)
			throws DataAccessException {
		Patient patient = (Patient) this.getSession().load(Patient.class, patientId);
		return patient;
	}

	@Override
	@Transactional
	public Patient updatePatient(Patient patient) throws DataAccessException {
		this.getHibernateTemplate().merge(patient);
		return patient;
	}

}
